/* 
 * Copyright 2012 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.appspot.dispotrains.station;

import com.google.common.collect.HashMultimap;
import com.google.common.collect.Lists;
import com.google.common.collect.Multimap;

import android.content.Context;
import android.util.Pair;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;


/**
 * @author etiennej@google.com (Etienne Membrives)
 *
 */
public class StationAdapter extends BaseExpandableListAdapter {
  private static final Comparator<Pair<Line, List<Station>>> LINE_COMPARATOR =
      new Comparator<Pair<Line, List<Station>>>() {
        @Override
        public int compare(Pair<Line, List<Station>> lhs, Pair<Line, List<Station>> rhs) {
          return lhs.first.toString().compareTo(rhs.first.toString());
        }
      };

  private static final Comparator<Station> STATION_COMPARATOR = new Comparator<Station>() {
    @Override
    public int compare(Station lhs, Station rhs) {
      return lhs.name.compareTo(rhs.name);
    }
  };

  private final List<Pair<Line, List<Station>>> stations;
  private final Context context;
  private final LayoutInflater layoutInflater;
  private final int expandableItemResource;
  private final int simpleItemResource;

  public StationAdapter(Context context, Map<Integer, Station> stationIdMap) {
    Multimap<Line, Station> stationMap = HashMultimap.<Line, Station>create();
    for (Station station : stationIdMap.values()) {
      for (Line line : station.getLines()) {
        stationMap.put(line, station);
      }
    }
    stations = Lists.newArrayList();
    for (Entry<Line, Collection<Station>> entry : stationMap.asMap().entrySet()) {
      List<Station> stationList = Lists.newArrayList(entry.getValue());
      Collections.sort(stationList, STATION_COMPARATOR);
      Pair<Line, List<Station>> pair = Pair.create(entry.getKey(), stationList);
      stations.add(pair);
    }
    Collections.sort(stations, LINE_COMPARATOR);
    this.context = context;
    layoutInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    expandableItemResource = android.R.layout.simple_expandable_list_item_1;
    simpleItemResource = android.R.layout.simple_list_item_1;
  }

  /**
   * @see android.widget.ExpandableListAdapter#getChild(int, int)
   */
  @Override
  public Object getChild(int groupPosition, int childPosition) {
    return stations.get(groupPosition).second.get(childPosition);
  }

  /**
   * @see android.widget.ExpandableListAdapter#getChildId(int, int)
   */
  @Override
  public long getChildId(int groupPosition, int childPosition) {
    return stations.get(groupPosition).second.get(childPosition).id;
  }

  /**
   * @see android.widget.ExpandableListAdapter#getChildView(int, int, boolean,
   *      android.view.View, android.view.ViewGroup)
   */
  @Override
  public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
      View convertView, ViewGroup parent) {
    TextView stationView;
    Station station = (Station) getChild(groupPosition, childPosition);
    if (convertView == null) {
      stationView = (TextView) layoutInflater.inflate(simpleItemResource, parent, false);
    } else {
      stationView = (TextView) convertView;
    }
    stationView.setText(station.name);
    return stationView;
  }

  /**
   * @see android.widget.ExpandableListAdapter#getChildrenCount(int)
   */
  @Override
  public int getChildrenCount(int groupPosition) {
    return stations.get(groupPosition).second.size();
  }

  /**
   * @see android.widget.ExpandableListAdapter#getGroup(int)
   */
  @Override
  public Object getGroup(int groupPosition) {
    return stations.get(groupPosition).first;
  }

  /**
   * @see android.widget.ExpandableListAdapter#getGroupCount()
   */
  @Override
  public int getGroupCount() {
    return stations.size();
  }

  /**
   * @see android.widget.ExpandableListAdapter#getGroupId(int)
   */
  @Override
  public long getGroupId(int groupPosition) {
    return stations.get(groupPosition).first.id;
  }

  /**
   * @see android.widget.ExpandableListAdapter#getGroupView(int, boolean,
   *      android.view.View, android.view.ViewGroup)
   */
  @Override
  public View getGroupView(
      int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
    TextView lineView;
    Line line = (Line) getGroup(groupPosition);
    if (convertView == null) {
      lineView = (TextView) layoutInflater.inflate(expandableItemResource, parent, false);
    } else {
      lineView = (TextView) convertView;
    }
    lineView.setText(line.toString());
    return lineView;
  }

  /**
   * @see android.widget.ExpandableListAdapter#hasStableIds()
   */
  @Override
  public boolean hasStableIds() {
    return true;
  }

  /**
   * @see android.widget.ExpandableListAdapter#isChildSelectable(int, int)
   */
  @Override
  public boolean isChildSelectable(int groupPosition, int childPosition) {
    return true;
  }
}
